

package com.hazelcast.topic.impl;

import com.hazelcast.cluster.Member;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.internal.serialization.SerializationService;
import com.hazelcast.topic.Message;

import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectOutputStream;
import java.io.Serial;

public class DataAwareMessage extends Message<Object> {

    @Serial
    private static final long serialVersionUID = 1;

    private final transient Data messageData;
    private final transient SerializationService serializationService;

    public DataAwareMessage(String topicName, Data messageData, long publishTime, Member publishingMember, SerializationService serializationService) {
        super(topicName, null, publishTime, publishingMember);
        this.serializationService = serializationService;
        this.messageData = messageData;
    }

    @Override
    public Object getMessageObject() {
        if (messageObject == null && messageData != null) {
            messageObject = serializationService.toObject(messageData);
        }
        return messageObject;
    }

    public Data getMessageData() {
        return messageData;
    }

    @Serial
    private void writeObject(ObjectOutputStream out) throws IOException {
        throw new NotSerializableException();
    }
}
